package org.jkuang.qstar.commons.jni;

import java.io.File;
import java.util.concurrent.atomic.AtomicInteger;


public  class Int64Set extends Native {
	private int module;

	private static AtomicInteger seq = new AtomicInteger();

	public Int64Set(int size) {
		this.module = seq.getAndIncrement();
		create(module, Math.abs(size) / 128 + 32);
	}

	public void add(long key) {
		add(module, key);
	}

	public void remove(long key) {
		remove(module, key);
	}

	public boolean contains(long key) {
		return contains(module, key);
	}

	public boolean readfile(String pathname) {
		File file = new File(pathname);
		if (file.exists() && file.length() > 1024 * 8) {
			return readfile(module, pathname);
		} else {
			return false;
		}
	}

	public boolean writefile(String pathname) {
		File file = new File(pathname);
		File dir = file.getParentFile();
		if (!dir.exists()) {
			dir.mkdirs();
		}
		return writefile(module, pathname);
	}

	public void destroy() {
		destroy(module);
	}

	@Override
	protected void finalize() throws Throwable {
		// TODO Auto-generated method stub
		super.finalize();
		this.destroy();
	}

	private native void create(int index, int hashmode);

	private native void destroy(int index);

	private native boolean add(int index, long key);

	private native boolean remove(int index, long key);

	private native boolean contains(int index, long key);

	private native boolean readfile(int index, String filename);

	private native boolean writefile(int index, String filename);

	private native int size(int index);
}
